//
//  DetaiController.m
//  BuyOfNoWane
//
//  Created by qianfeng02 on 15/7/6.
//  Copyright (c) 2015年 迷茫的尧尧. All rights reserved.
//

#import "DetaiController.h"
#import "AFNetworking.h"
#import "AppModel.h"
#import "UIImageView+WebCache.h"
#import "ILBarButtonItem.h"
#import "MMProgressHUD.h"
#import "UMSocial.h"
@interface DetaiController ()<UMSocialUIDelegate>
@property (nonatomic ,strong)UIWebView *webView;
@property (nonatomic,strong)AFHTTPRequestOperationManager *manager;
@end

@implementation DetaiController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self createUI];
    
   
}
-(void)createUI
{
    ILBarButtonItem *buybtn =
    [ILBarButtonItem barItemWithImage:[UIImage imageNamed:@"cutmypic"]
                        selectedImage:[UIImage imageNamed:@"cutmypic"]
                               target:self
                               action:@selector(buybtnClick:)];
    ILBarButtonItem *sharebtn =
    [ILBarButtonItem barItemWithImage:[UIImage imageNamed:@"ic_xq_share_pressed"]
                        selectedImage:[UIImage imageNamed:@"ic_xq_share_pressed"]
                               target:self
                               action:@selector(sharebtnClick:)];
    ILBarButtonItem *backbtn=
    [ILBarButtonItem barItemWithImage:[UIImage imageNamed:@"ic_back_s"]
                        selectedImage:[UIImage imageNamed:@"ic_back_s"]
                               target:self
                               action:@selector(backbtnClick:)];
    
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:buybtn,sharebtn, nil]];
    [self.navigationItem setLeftBarButtonItem:backbtn];
}
-(void)backbtnClick:(UIButton *)button
{
   // DetaiController *detail=[[DetaiController alloc]init];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 购买页面
-(void)buybtnClick:(UIButton *)button
{
    NSString *url=[NSString stringWithFormat:kUrl,self.categoryType,self.categoryID];
    [ [UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
#pragma mark 分享
-(void)sharebtnClick:(UIButton *)button
{
    //分享
    NSString *text = [NSString stringWithFormat:@"这个东西不错，俺买了!%@",self.article_url];
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010"
                                      shareText:text
                                     shareImage:nil
                                shareToSnsNames:@[UMShareToSina,UMShareToSms,UMShareToEmail,UMShareToQQ,UMShareToWechatTimeline]
                                       delegate:self];
}
#pragma mark UM-delegate
-(BOOL)isDirectShareInIconActionSheet
{
    return YES;
}

-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}
-(void)initUI
{
    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleExpand];
    [MMProgressHUD showWithTitle:@"欢迎光临" status:@"Loading..."];
    _webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+49)];
    _webView.backgroundColor=[UIColor darkGrayColor];
    [self.view addSubview:_webView];
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url=nil;
    if ([self.categoryType isEqualToString:kSelectedType]) {
        switch ([_article_channel_id intValue]) {
            case 1:
            {
                _categoryType=kDiscountType;
            }
                break;
            case 5:
            {
                 _categoryType=kBuyoverseaType;
            }
                break;
            case 6:
            {
                 _categoryType=kFoundType;
            }
                break;
        }
        url=[NSString stringWithFormat:kDetailUrl,self.categoryType,self.categoryID];
    }else{
     url=[NSString stringWithFormat:kDetailUrl,self.categoryType,self.categoryID];
    }
//    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:self.article_url]];
//    [_webView loadRequest:request];
//    [MMProgressHUD dismissWithError:nil title:@"网络故障"];
   
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dict2=dict[@"data"];
        AppModel *model=[[AppModel alloc]init];
        [model setValuesForKeysWithDictionary:dict2];
        NSString *str=dict2[@"article_filter_content"];
        [self.webView loadHTMLString:str baseURL:nil];
        [MMProgressHUD dismissWithSuccess:@"OK" title:@"数据下载完成"];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MMProgressHUD dismissWithError:nil title:@"网络故障"];
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden=NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
